<?php
/**
 * This file contains the class PersonDB which provides
 * all of the database requirements for a person
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 */
/**
 * This class provides access to the database for PersonDB
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 */
class PersonDB
{
    /**
      * Provides access to the database
     *
     * @var Db
     */
    private $_db;
    /**
     * Create a new instance of this class
     * 
     * @return void
     * @param Db $db
     */
    function __construct($db)
    {
        $this->_db = $db;
    }
    /**
     * Escape the provided value
     *
     * @return string
     * @param mixed $value
     */
    function escape($value)
    {
        $db = $this->_db;
        return $db->escape($value);
    }
    /**
     * Retrieve a persons details
     *
     * @return array[string]mixed
     * @param int $personId
     */
    function getPerson($personId)
    {
        $db = $this->_db;
        $person = Array();
        $sql = <<< EOT
            SELECT
                Person.ID, 
                Person.Surname, 
                Person.FirstNames, 
                Person.FormerName, 
                Person.NeeName, 
                Person.AlsoKnownAs, 
                Person.DeathDate, 
                Person.BirthDate, 
                Person.Age AS Age, 
                Person.AgeMeasure AS AgeMeasure, 
                Country.Name AS Country
            FROM
                Person 
                    LEFT JOIN Country
                        ON Person.CountryID = Country.ID
            WHERE
                Person.ID = %d
EOT;
        $sql = sprintf($sql, $personId);
        $result = $db->query($sql);
        if ($db->rowCount($result) > 0) {
            $row = $db->fetch($result);
            $person['id'] = $row['ID'];
            $person['surname'] = $row['Surname'];
            $person['firstNames'] = $row['FirstNames'];
            $person['formerName'] = $row['FormerName'];
            $person['neeName'] = $row['NeeName'];
            $person['alsoKnownAs'] = $row['AlsoKnownAs'];
            $person['deathDate'] = $row['DeathDate'];
            $person['age'] = $row['Age'];
            $person['ageMeasure'] = $row['AgeMeasure'];
            $person['country'] = $row['Country'];
        }
        return $person;
    }
}
?>